package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.admin.FuncionalidadDTO;
import py.com.alfa.escriba.gui.dto.admin.PerfilDTO;
import py.com.alfa.escriba.gui.dto.admin.UsuarioDTO;
import py.com.alfa.escriba.gui.rest.services.AdministracionService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;

@SuppressWarnings({ "unchecked", "unused"})
public class AdministracionServiceClient implements AdministracionService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	public AdministracionServiceClient() {}

	/******* USUARIO *******/
	@Override
	public UsuarioDTO validaUsuario(String user, String clave) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		UsuarioDTO dto = new UsuarioDTO();
		dto.setUsername(user);
		dto.setPassword(clave);
		request.addBody(dto);
		ResponseEntity<UsuarioDTO> response = (ResponseEntity<UsuarioDTO>) request.exchange(VALIDA_USUARIO, HttpMethod.POST, UsuarioDTO.class);
		dto = response.getBody();
		return dto;
	}
	
	@Override
	public List<UsuarioDTO> listUsuario() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<UsuarioDTO[]> response = (ResponseEntity<UsuarioDTO[]>) request.exchange(LIST_USUARIO, HttpMethod.GET, UsuarioDTO[].class);
		log.info("OK - obtenerUsuarios ");
		List<UsuarioDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public UsuarioDTO createUsuario(UsuarioDTO usuario) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(usuario);
		ResponseEntity<UsuarioDTO> response = (ResponseEntity<UsuarioDTO>) request.exchange(LIST_USUARIO, HttpMethod.POST, UsuarioDTO.class);
		log.info("OK - createUsuario ");
		UsuarioDTO result = response.getBody();
		return result;
	}

	@Override
	public UsuarioDTO saveUsuario(UsuarioDTO usuario) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", usuario.getId().toString());
		request.addBody(usuario);
		ResponseEntity<UsuarioDTO> response = (ResponseEntity<UsuarioDTO>) request.exchange(MB_USUARIO, HttpMethod.PUT, UsuarioDTO.class);
		log.info("OK - saveUsuario ");
		UsuarioDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteUsuario(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_USUARIO, HttpMethod.DELETE, null);
		log.info("OK - deleteUsuario ");
	}

	@Override
	public UsuarioDTO showUsuario(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<UsuarioDTO> response = (ResponseEntity<UsuarioDTO>) request.exchange(MB_USUARIO, HttpMethod.GET, UsuarioDTO.class);
		log.info("OK - showUsuario ");
		UsuarioDTO result = response.getBody();
		return result;
	}
	/******* FUNCIONALIDAD *******/
	@Override
	public List<FuncionalidadDTO> listFuncionalidad() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<FuncionalidadDTO[]> response = (ResponseEntity<FuncionalidadDTO[]>) request.exchange(LIST_FUNCIONALIDAD, HttpMethod.GET, FuncionalidadDTO[].class);
		log.info("OK - obtenerFuncionalidades ");
		List<FuncionalidadDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public FuncionalidadDTO createFuncionalidad(FuncionalidadDTO funcionalidad) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(funcionalidad);
		ResponseEntity<FuncionalidadDTO> response = (ResponseEntity<FuncionalidadDTO>) request.exchange(LIST_FUNCIONALIDAD, HttpMethod.POST, FuncionalidadDTO.class);
		log.info("OK - createFuncionalidad ");
		FuncionalidadDTO result = response.getBody();
		return result;
	}

	@Override
	public FuncionalidadDTO saveFuncionalidad(FuncionalidadDTO funcionalidad) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", funcionalidad.getId().toString());
		request.addBody(funcionalidad);
		ResponseEntity<FuncionalidadDTO> response = (ResponseEntity<FuncionalidadDTO>) request.exchange(MB_FUNCIONALIDAD, HttpMethod.PUT, FuncionalidadDTO.class);
		log.info("OK - saveFuncionalidad ");
		FuncionalidadDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteFuncionalidad(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_FUNCIONALIDAD, HttpMethod.DELETE, null);
		log.info("OK - deleteFuncionalidad ");
	}

	@Override
	public FuncionalidadDTO showFuncionalidad(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<FuncionalidadDTO> response = (ResponseEntity<FuncionalidadDTO>) request.exchange(MB_FUNCIONALIDAD, HttpMethod.GET, FuncionalidadDTO.class);
		log.info("OK - showFuncionalidad ");
		FuncionalidadDTO result = response.getBody();
		return result;
	}
	/******* PERFIL *******/
	@Override
	public List<PerfilDTO> listPerfil() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<PerfilDTO[]> response = (ResponseEntity<PerfilDTO[]>) request.exchange(LIST_PERFIL, HttpMethod.GET, PerfilDTO[].class);
		log.info("OK - obtenerPerfiles ");
		List<PerfilDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public PerfilDTO createPerfil(PerfilDTO perfil) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(perfil);
		ResponseEntity<PerfilDTO> response = (ResponseEntity<PerfilDTO>) request.exchange(LIST_PERFIL, HttpMethod.POST, PerfilDTO.class);
		log.info("OK - createPerfil ");
		PerfilDTO result = response.getBody();
		return result;
	}

	@Override
	public PerfilDTO savePerfil(PerfilDTO perfil) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", perfil.getId().toString());
		request.addBody(perfil);
		ResponseEntity<PerfilDTO> response = (ResponseEntity<PerfilDTO>) request.exchange(MB_PERFIL, HttpMethod.PUT, PerfilDTO.class);
		log.info("OK - savePerfil ");
		PerfilDTO result = response.getBody();
		return result;
	}

	@Override
	public void deletePerfil(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_PERFIL, HttpMethod.DELETE, null);
		log.info("OK - deletePerfil ");
	}

	@Override
	public PerfilDTO showPerfil(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<PerfilDTO> response = (ResponseEntity<PerfilDTO>) request.exchange(MB_PERFIL, HttpMethod.GET, PerfilDTO.class);
		log.info("OK - showPerfil ");
		PerfilDTO result = response.getBody();
		return result;
	}
}
